/* Nothing interesting here, just testing the arc function...
 * There bunch of points in top left stacked on top of each other...
 * You can drag them around and see what they do I guess...
 */

#include "common/sketchbook.hpp"

constexpr float corner_radius = 14.f;
constexpr float2 half = float2::one(.5f);
const float tau = 2*std::acos(-1);


struct point
{
	enum
	{
		center,
		center2,

		lower,
		upper,

		count
	};
};

std::array<float2, point::count> points{};

float2* dragged_point = nullptr;

bool is_near(float2 corner, float2 position);


void start(Program& program)
{

	// program.fullscreen = true;

	program.draw_once = [](auto frame)
	{
		points[point::center] = trand_float2() * frame.size;
		points[point::center2] = trand_float2() * frame.size;
		points[point::lower] = trand_float2() * frame.size;
		points[point::upper] = trand_float2() * frame.size;
		// points[point::lower] = frame.size/2 - float2::i(30);
		// points[point::upper] = frame.size/2 - float2::i(30);
	};

	program.key_up = [&program](scancode code, keycode)
	{
		switch(code)
		{
			case scancode::leftbracket:
			case scancode::c:
				if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
			case scancode::escape:
				program.end();
			break;

			default: break;
		}
	};

	program.mouse_down = [](float2 position, auto)
	{
		for(int i = 0; i < point::count; ++i)
			if(is_near(points[i], position))
				dragged_point = &points[i];
	};

	program.mouse_up = [](auto, auto)
	{
		dragged_point = nullptr;
	};

	program.mouse_move = [](auto, float2 motion)
	{
		if(dragged_point)
			(*dragged_point) += motion;
	};

	program.draw_loop = [](auto frame, auto)
	{

		frame.begin_sketch()
			.rectangle(rect{ frame.size })
			.fill(0x0_rgb)
		;

		points[point::upper] = points[point::center] + common::normalize(points[point::upper] - points[point::center]) * (points[point::lower] - points[point::center]).length();
		points[point::center2] = points[point::center] + common::project(points[point::center2] - points[point::center], support::halfway(points[point::lower], points[point::upper]) - points[point::center]);

		frame.begin_sketch()
			.arc(points[point::center], range2f{points[point::lower], points[point::upper]} - points[point::center], 1)
			.sector(points[point::center2], range2f{points[point::upper], points[point::lower]} - points[point::center2], -1)
			.line_width(5).outline(0x7700ff_rgb)
			.fill(0xffffff_rgb)
		;

		{ auto sketch = frame.begin_sketch();
			for(int i = 0; i < point::count; ++i)
				sketch.ellipse(rect{float2::one(corner_radius), points[i], half});
			sketch.line_width(1).outline(0x999999_rgb);
		}

	};
}

bool is_near(float2 corner, float2 position)
{
	return (corner - position).magnitude() < corner_radius * corner_radius;
}
